package com.aidansmeaton;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.Toast;

public class SelectGameActivity extends WebViewActivity {
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		url = "http://www.airwaysim.com/game";
		loadWebView();
	
	}
	
	@Override
	protected void loadWebView() {
				
		webview = (WebView) findViewById(R.id.webview);
		webview.getSettings().setJavaScriptEnabled(true);
		webview.addJavascriptInterface(new SelectGameJavaScriptInterface(), "HTMLOUT");
		webview.getSettings().setSaveFormData(true);
		webview.getSettings().setBuiltInZoomControls(true);
		webview.setWebViewClient(new SelectGameWebViewClient());
		webview.loadUrl(url); // 'html' instance variable is also loaded here
		
	}
	
	private void goToDashboard() {
		Intent i = new Intent(getBaseContext(), DashboardActivity.class);
        startActivity(i);
        finish();
	}
	
	/* An instance of this class will be registered as a JavaScript interface */
	protected class SelectGameJavaScriptInterface {
		
	    public void showHTML(String pageHTML) {
	    	
	    	html = pageHTML;
	    	Log.w("GAME",pageHTML);
	    	
	    	if (html.contains("Change game")){
	    		goToDashboard();
			}	    	
	    }
	}
		
	private class SelectGameWebViewClient extends HTMLWebViewClient {
		
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			Toast.makeText(getApplicationContext(), "SelectGameActivity: " + url, Toast.LENGTH_LONG).show();
			view.loadUrl(url);  
			return true;
		}
		
	}
		
}